﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace hasApopo.view
{
    public partial class DigitalSignature : Form
    {

        Boolean pBenar = false;
        Boolean qBenar = false;
        Boolean eBenar = false;

        byte[] isifile;
        string filename;
        byte[] signature;

        public DigitalSignature()
        {
            InitializeComponent();
        }

        private void DigitalSignature_Load(object sender, EventArgs e)
        {

        }

        /*** bagian key generator ***/
        #region key generator
        
        private void trueP_Click(object sender, EventArgs e)
        {
            //kalo bener bilangan prima, maka true bakal muncul
            if (pText.Text == "halo")
            {
                trueP.Visible = true;
                pBenar = true;
            }
            else
                trueP.Visible = false;
        }       

        private void trueQ_Click(object sender, EventArgs e)
        {
            //kalo bener bilangan prima, maka true bakal muncul
            if (qText.Text == "halo")
            {
                trueQ.Visible = true;
                qBenar = true;
            }
            else
                trueQ.Visible = false;
        }

        private void trueE_Click(object sender, EventArgs e)
        {
            //kalo bener bilangan relatif prima, maka true bakal muncul
            if (eText.Text == "halo")
            {
                trueE.Visible = true;
                eBenar = true;
            }
            else
                trueE.Visible = false;
        }

        private void generateKeyButton_Click(object sender, EventArgs e)
        {
            if (eBenar && pBenar && qBenar)
            {
                trueE.Visible = true;
                trueP.Visible = true;
                trueQ.Visible = true;
            }
            else
                MessageBox.Show("Popo berkata : Masukan salah ih!");
        }

        private void savepublicButton_Click(object sender, EventArgs e)
        {
            
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            //saveFileDialog1.Title = "Save Result";
            //saveFileDialog1.ShowDialog();
            //TextWriter tw = new StreamWriter();

            //if (saveFileDialog1.FileName!="")
            //{
            //    tw.WriteLine(teksE);
            //    tw.WriteLine(teksNpublic);    
            //}


        }

        private void saveprivateButton_Click(object sender, EventArgs e)
        {
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            //saveFileDialog1.Title = "Save Result";
            //saveFileDialog1.ShowDialog();
            //TextWriter tw = new StreamWriter();

            //if (saveFileDialog1.FileName != "")
            //{
            //    tw.WriteLine(teksD);
            //    tw.WriteLine(teksNprivate);
            //}
        }
        #endregion


        /*** bagian digital signature ***/

        private void browseButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Title = "Open File";
            DialogResult d = o.ShowDialog();

            if(d == DialogResult.OK){
                filename = o.FileName;
                pathFileText1.Text = filename;

                isifile = File.ReadAllBytes(filename);

                fileText1.Text = bytetostring(isifile);
                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //save signature
            if (inFile.Checked)
            {
                //save sugnature in same document
                SaveFileDialog s = new SaveFileDialog();
                s.Title = "Save File with Signature";
                DialogResult d = s.ShowDialog();
                
                if (d == DialogResult.OK)
                {
                    BinaryWriter bw = new BinaryWriter(File.OpenWrite(s.FileName));
                    bw.Write(isifile);
                    bw.Write(0x1A);     //end of file
                    bw.Write("<ds>"+signature+"</ds>");
                    bw.Close();
                }
            }
            else if (separated.Checked)
            {
                //save signature in another document
                SaveFileDialog s = new SaveFileDialog();
                s.Title = "Save Signature";
                DialogResult d = s.ShowDialog();

                if (d == DialogResult.OK)
                {
                    BinaryWriter bw = new BinaryWriter(File.OpenWrite(s.FileName));
                    bw.Write("nama file: "+filename+"\n");
                    bw.Write("signature: "+signature);
                    bw.Close();
                }
            }
            else
            {
                MessageBox.Show("Please select signature location");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog o = new OpenFileDialog();
            o.Title = "Open File";
            DialogResult d = o.ShowDialog();

            if (d == DialogResult.OK)
            {
                filename = o.FileName;
                pathFileText2.Text = filename;
                byte[] isifiletemp;

                isifiletemp = File.ReadAllBytes(filename);

                if (isifiletemp[isifiletemp.Length - 5] == (byte)'<'
                    && isifiletemp[isifiletemp.Length - 4] == (byte)'/'
                    && isifiletemp[isifiletemp.Length - 3] == (byte)'d'
                    && isifiletemp[isifiletemp.Length - 2] == (byte)'s'
                    && isifiletemp[isifiletemp.Length - 1] == (byte)'>'
                    )
                {
                    //asumsi: setelah end of file pasti cuma ada digital signature aja

                    int j = 0;
                    while (isifiletemp[j] != 0x1A)
                        j++;    //hitung panjang

                    isifile = new byte[j];

                    j = 0;
                    while (j < isifile.Length)
                    {
                        isifile[j] = isifiletemp[j];
                        j++;
                    }

                    //cari signature
                    bool isSignature = false;
                    while (j < isifiletemp.Length && !isSignature)
                    {
                        if (isifiletemp[j] == (byte)'<'
                            && isifiletemp[j + 1] == (byte)'d'
                            && isifiletemp[j + 2] == (byte)'s'
                            && isifiletemp[j + 3] == (byte)'>'
                            )
                        {
                            isSignature = true;
                        }
                        else
                        {
                            j++;
                        }
                    }

                    j += 4; //lompat ke awal signature

                    //baca signature
                    signature = new byte[isifiletemp.Length - 5 - j];
                    int k = 0;
                    while (k < signature.Length)
                    {
                        signature[k] = isifiletemp[j];
                        j++;
                        k++;
                    }

                    inFile2.Checked = true;
                }
                else
                {
                    isifile = isifiletemp;
                    signature = null;
                }

                fileText2.Text = bytetostring(isifile);
                signatureText.Text = bytetostring(signature);
            }
        }

        string bytetostring(byte[] b)
        {
            if (b == null)  //kasus kosong
                return "";
            else
            {
                StringBuilder s = new StringBuilder();
                for (int i = 0; i < b.Length; i++)
                {
                    s.Append((char)b[i]);
                }

                return s.ToString();
            }
        }


        
        private void button2_Click(object sender, EventArgs e)
        {
            //load key from file

            //tampilkan
        }

        private void SignatureButton_Click(object sender, EventArgs e)
        {
            //make signature

            //tampilkan
            signatureText.Text = bytetostring(signature);
        }

        private void inFile2_CheckedChanged(object sender, EventArgs e)
        {
            button5.Enabled = false;
        }

        private void separated2_CheckedChanged(object sender, EventArgs e)
        {
            button5.Enabled = true;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //browse signature
        }

    }
}
